package com.liuyanzhao.blog.controller.Admin;

import com.liuyanzhao.blog.entity.Article;
import com.liuyanzhao.blog.entity.custom.*;
import com.liuyanzhao.blog.service.ArticleService;
import com.liuyanzhao.blog.service.CategoryService;
import com.liuyanzhao.blog.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 后台文章管理模块
 * @author Anbing Cui
 * @CrateTime: 2019年6月22日
 */
@Controller
@RequestMapping("/admin/article")
public class BackArticleController {
    @Autowired
    private ArticleService articleService;

    @Autowired
    private TagService tagService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 
     *  View all articles
     * @throws Exception
     * @Return: ModelAndView
     * @author Anbing Cui
     * @CreateDate: 2019年6月22日 下午3:24:31
     */
    @RequestMapping(value = "")
    public ModelAndView index() throws Exception {
        ModelAndView modelAndView = new ModelAndView();

        //分页显示已发布文章
        Integer pageSize = 5;
        List<ArticleListVo> publishedArticleListVoList = articleService.listArticleByPage(1,null,pageSize);
        modelAndView.addObject("publishedArticleListVoList",publishedArticleListVoList);

        //不分页显示 草稿文章
        List<ArticleListVo> draftArticleList = articleService.listArticle(0);
        modelAndView.addObject("draftArticleList",draftArticleList);
        modelAndView.setViewName("Admin/Article/index");
        return modelAndView;
    }

    /**
     * 
     *  Article paging query
     * @param pageNow
     * @throws Exception
     * @Return: ModelAndView
     * @author Anbing Cui
     * @CreateDate: 2019年6月22日 下午3:25:20
     */
    @RequestMapping("/p/{pageNow}")
    public @ResponseBody  ModelAndView ArticleListByPageView(@PathVariable("pageNow") Integer pageNow) throws Exception{
        ModelAndView modelAndView = new ModelAndView();
        //分页显示已发布文章
        Integer pageSize = 20;
        List<ArticleListVo> publishedArticleListVoList = articleService.listArticleByPage(1,pageNow,pageSize);
        modelAndView.addObject("publishedArticleListVoList",publishedArticleListVoList);

        //不分页显示 草稿文章
        List<ArticleListVo> draftArticleList = articleService.listArticle(0);
        modelAndView.addObject("draftArticleList",draftArticleList);
        modelAndView.setViewName("Admin/Article/index");
        return modelAndView;
    }

    /**
     * 
     * Data preparation before writing an article
     * @return
     * @throws Exception
     * @Return: ModelAndView
     * @author Anbing Cui
     * @CreateDate: 2019年6月22日 下午3:25:24
     */
    @RequestMapping(value = "/insert")
    public ModelAndView insertArticleView() throws Exception {
        ModelAndView modelAndView = new ModelAndView();

        List<CategoryCustom> categoryCustomList = categoryService.listCategory(1);
        List<TagCustom> tagCustomList = tagService.listTag(1);

        modelAndView.addObject("categoryCustomList",categoryCustomList);
        modelAndView.addObject("tagCustomList",tagCustomList);

        modelAndView.setViewName("Admin/Article/insert");
        return modelAndView;
    }

    /**
     * 
     *  Insert a new article
     * @param article 文章信息
     * @throws Exception
     * @Return: String  返回到全部文章界面
     * @author Anbing Cui
     * @CreateDate: 2019年6月22日 下午3:25:28
     */
    @RequestMapping(value = "/insertSubmit",method = RequestMethod.POST)
    public String insertArticleSubmit(Article article) throws Exception {
        article.setArticlePostTime(new Date());
        article.setArticleUpdateTime(new Date());
        article.setArticleIsComment(1);
        article.setArticleViewCount(0);
        article.setArticleLikeCount(0);
        article.setArticleCommentCount(0);
        article.setArticleStatus(1);
        article.setArticleOrder(1);
        articleService.insertArticle(article);
        return "redirect:/admin/article";
    }

    /**
     * 
     *  Save the article as a draft
     * @param article  文章详细信息
     * @throws Exception
     * @Return: String   返回到全部文章页面
     * @author Anbing Cui
     * @CreateDate: 2019年6月22日 下午3:25:33
     */
    @RequestMapping(value = "/insertDraftSubmit",method = RequestMethod.POST)
    public String insertArticleDraftSubmit(Article article) throws Exception {

        article.setArticlePostTime(new Date());
        article.setArticleUpdateTime(new Date());
        article.setArticleIsComment(1);
        article.setArticleViewCount(0);
        article.setArticleLikeCount(0);
        article.setArticleCommentCount(0);
        article.setArticleStatus(0);
        article.setArticleOrder(1);

        articleService.insertArticle(article);

        return "redirect:/admin/article";
    }

    /**
     * 
     *  Query the article according to the query content
     * @param request
     * @param model
     * @throws Exception
     * @Return: ModelAndView  查询结果封装
     * @author Anbing Cui
     * @CreateDate: 2019年6月22日 下午3:25:37
     */
    @RequestMapping("/search")
    @ResponseBody
    public ModelAndView SearchPageView(HttpServletRequest request,Model model) throws Exception {
        ModelAndView modelAndView = new ModelAndView();
        //设置每页显示的数量
        int pageSize = 20;
        String query = request.getParameter("query");
        List<ArticleSearchVo> articleSearchVoList = articleService.listSearchResultByPage(1,request,model,null,pageSize,query);
        modelAndView.addObject("articleSearchVoList", articleSearchVoList);
        modelAndView.setViewName("Admin/Article/search");
        return modelAndView;
    }

    /**
     * 
     *  Query for articles and paginate them
     * @param request
     * @param model
     * @param pageNow  页数
     * @throws Exception
     * @Return: ModelAndView
     * @author Anbing Cui
     * @CreateDate: 2019年6月22日 下午3:25:44
     */
    @RequestMapping("/p/{pageNow}/search")
    @ResponseBody
    public  ModelAndView SearchPageByPageView(HttpServletRequest request, Model model,@PathVariable("pageNow") Integer pageNow) throws Exception {
        ModelAndView modelAndView = new ModelAndView();
        //设置每页显示的数量
        int pageSize = 4;
        String query = request.getParameter("query");
        List<ArticleSearchVo> articleSearchVoList = articleService.listSearchResultByPage(1,request,model,pageNow,pageSize,query);
        modelAndView.addObject("articleSearchVoList", articleSearchVoList);
        modelAndView.setViewName("/Admin/Article/search");
        return modelAndView;
    }

    /**
     * 
     *  Delete the article based on its ID
     * @param id    文章的ID
     * @throws Exception
     * @Return: 无返回值
     * @author Anbing Cui
     * @CreateDate: 2019年6月22日 下午3:25:50
     */
    @RequestMapping(value = "/delete/{id}")
    public void deleteArticle(@PathVariable("id") Integer id) throws Exception {
        //调用service批量删除
        articleService.deleteArticle(id);
    }

    /**
     * 
     * Bulk deletion of articles
     * @param request
     * @throws Exception
     * @Return: void
     * @author Anbing Cui
     * @CreateDate: 2019年6月22日 下午3:25:56
     */
    @RequestMapping(value = "/deleteBatch")
    public void deleteArticles(HttpServletRequest request) throws Exception {
        String str = request.getParameter("ids");
        String[] arr = str.split(",");
        Integer[] ids = new Integer[arr.length];
        for(int i=0;i<arr.length;i++) {
            ids[i] = Integer.valueOf(arr[i]);
        }
        //调用service批量删除
        articleService.deleteArticleBatch(ids);

    }
    /**
     * 
     *  Edit the article according to its ID
     * @param id 文章的ID
     * @throws Exception
     * @Return: ModelAndView  封装了查询的文章信息，返回到文章编辑页面
     * @author Anbing Cui
     * @CreateDate: 2019年6月22日 下午3:26:00
     */
    @RequestMapping(value = "/edit/{id}")
    public ModelAndView editArticleView(@PathVariable("id") Integer id) throws Exception {
        ModelAndView modelAndView = new ModelAndView();

        ArticleCustom articleCustom =  articleService.getArticleById(null,id);
        modelAndView.addObject("articleCustom",articleCustom);

        List<CategoryCustom> categoryCustomList = categoryService.listCategory(1);
        modelAndView.addObject("categoryCustomList",categoryCustomList);

        List<TagCustom> tagCustomList = tagService.listTag(1);
        modelAndView.addObject("tagCustomList",tagCustomList);


        modelAndView.setViewName("Admin/Article/edit");
        return modelAndView;
    }

    /**
     * 
     *  Data preparation before revision - prepare raw data for clerical staff
     * @param articleCustom   文章信息
     * @throws Exception
     * @Return: String   返回到全部文章列表
     * @author Anbing Cui
     * @CreateDate: 2019年6月22日 下午3:26:07
     */
    @RequestMapping(value = "/editSubmit",method = RequestMethod.POST)
    public String editArticleSubmit(ArticleCustom articleCustom) throws Exception {
        Integer id = articleCustom.getArticleId();
        articleCustom.setArticleUpdateTime(new Date());
        articleService.updateArticle(id,articleCustom);
        return "redirect:/admin/article";
    }

}



